package action.ManagerAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.CroPropaganda;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroPropagandaService;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * Created by Administrator on 2017/5/14 0014.
 */
public class PropagandaAction extends ActionSupport {
    private String id;
    private String propagandaname;
    private String url;
    private String photo;
    private String type;
    private CroPropaganda croPropagandaVo;
    private List<CroPropaganda> croPropaganda;
    private CroPropaganda recroPropaganda;
    private String successResult;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 10;
    private String jsonObject;
    @Autowired
    private CroPropagandaService croPropagandaService;


    /*
  * 多属性搜索
  * */
    @RequiresPermissions("user:select")
    public String querylist(){
        Pagination<CroPropaganda> page;
        if(pageIndex==0){
            page=croPropagandaService.FindUserByProperty(croPropagandaVo,1,pageSize);
        }else {
            page=croPropagandaService.FindUserByProperty(croPropagandaVo,pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setRecroPropaganda(croPropagandaVo);
        jsonObject= JSON.toJSONString(recroPropaganda);
        setCroPropaganda(page.getItems());
        return SUCCESS;
    }
    /*
   * 分页展示宣传信息
   * */
    @RequiresPermissions("user:select")
    public String showdetail(){
        Pagination<CroPropaganda> page;
        if(pageIndex==0){
            page=croPropagandaService.findAllUser(1,pageSize);
        }else{
            page=croPropagandaService.findAllUser(pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setCroPropaganda(page.getItems());
        return SUCCESS;
    }

    @RequiresPermissions("user:delete")
    /*删除*/
    public String delPropaganda(){
        if(croPropagandaService.deletePropaganda(id)){
            ServletContext context= ServletActionContext.getServletContext();
            context.setAttribute("friendurl",croPropagandaService.listfriendurl());
            successResult = "删除成功";
        }else {
            successResult = "删除失败";
        }
        return SUCCESS;
    }

    @RequiresPermissions("user:write")
    /*添加*/
    public String addPropaganda(){
        CroPropaganda croPropaganda = new CroPropaganda();
        croPropaganda.setType(type);
        croPropaganda.setPropagandaname(propagandaname);
        croPropaganda.setUrl(url);
        if(photo!=null){
            croPropaganda.setPhoto(photo);
        }
        if(croPropagandaService.addPropaganda(croPropaganda)){
            ServletContext context= ServletActionContext.getServletContext();
            context.setAttribute("friendurl",croPropagandaService.listfriendurl());
            successResult = "添加成功";
        }else {
            successResult = "添加失败";
        }
        return SUCCESS;
    }

    @RequiresPermissions("user:select")
    /*根据id查找信息*/
    public String listPropaganda(){
        croPropagandaVo = croPropagandaService.FindPropagandaByid(id);
        return "updatepropaganda";
    }

    @RequiresPermissions("user:update")
    /*修改*/
    public String updatePropaganda(){
        System.out.println(id);
        System.out.println(propagandaname);
        System.out.println(url);
        System.out.println(type);
        CroPropaganda croPropaganda = croPropagandaService.FindPropagandaByid(id);
        croPropaganda.setPropagandaname(propagandaname);
        croPropaganda.setUrl(url);
        croPropaganda.setType(type);
        if(photo!=null){
            croPropaganda.setPhoto(photo);
        }
        if(croPropagandaService.updatePropaganda(croPropaganda)){
            ServletContext context= ServletActionContext.getServletContext();
            context.setAttribute("friendurl",croPropagandaService.listfriendurl());
            successResult = "修改成功";
        }else {
            successResult = "修改失败";
        }
        return SUCCESS;
    }






    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getJsonObject() {
        return jsonObject;
    }

    public void setJsonObject(String jsonObject) {
        this.jsonObject = jsonObject;
    }

    public CroPropaganda getRecroPropaganda() {
        return recroPropaganda;
    }

    public void setRecroPropaganda(CroPropaganda recroPropaganda) {
        this.recroPropaganda = recroPropaganda;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPropagandaname() {
        return propagandaname;
    }

    public void setPropagandaname(String propagandaname) {
        this.propagandaname = propagandaname;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public CroPropaganda getCroPropagandaVo() {
        return croPropagandaVo;
    }

    public void setCroPropagandaVo(CroPropaganda croPropagandaVo) {
        this.croPropagandaVo = croPropagandaVo;
    }

    public List<CroPropaganda> getCroPropaganda() {
        return croPropaganda;
    }

    public void setCroPropaganda(List<CroPropaganda> croPropaganda) {
        this.croPropaganda = croPropaganda;
    }
}
